#include "button.h"
#include <QPainter>
#include <QPaintEvent>

Button::Button(QWidget *parent) :
    QPushButton(parent), m_isCross(true), m_clicked(false)
{
    setMinimumSize(QSize(50, 50));
    setMaximumSize(QSize(200, 200));
    this->setPalette( QPalette( Qt::white ) );
    this->setAutoFillBackground( true );
}

bool Button::globalCross = true;

int Button::getState()
{
    if (!m_clicked) return 100;
    if (m_isCross) return 1;
    return 0;
}

QSize Button::sizeHint()
{
    static QSize size(50, 50);
    return size;
}

void Button::paintEvent(QPaintEvent *e)
{
    if (this->isDown() && !m_clicked)
    {
        m_isCross = globalCross;
        m_clicked = true;
        globalCross = !globalCross;
    }
    if (m_clicked)
    {
        QPainter painter(this);
        painter.setBrush(Qt::black);
        int xcorner;
        int ycorner;
        int height;
        int width;

        e->rect().getRect(&xcorner, &ycorner, &width, &height);
        int xcenter = xcorner + width/2;
        int ycenter = ycorner + height/2;
        int radius = height < width ? height/4 : width/4;
        if (m_isCross)
        {
            QPen pen;
            pen.setWidth(5);
            painter.setPen(pen);
            QLine line1(xcenter - radius/2, ycenter - radius, xcenter + radius, ycenter + radius);
            QLine line2(xcenter - radius/2, ycenter + radius, xcenter + radius, ycenter - radius);
            painter.drawLine(line1);
            painter.drawLine(line2);
        }
        else
        {
            QPoint center(xcenter, ycenter);
            painter.drawEllipse(center, radius, radius);
        }
    }
}
